home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / castools.zip / FAXSEND.C < prev    next >
Text File  |  1990-01-05  |  15KB  |  412 lines

  1. /*
  2.    FAXSEND.C  The first high-level CAS Toolkit function.
  3.  
  4.    This function submits a send task to the Resident Manager of the
  5.    communications hardware.  It sets communications options using its own
  6.    parameters in combination with the global variable DefaultsECS.
  7.  
  8.    INPUT:  The global Event Control structure DefaultsECS and parameters for
  9.            "who, what, and when" that override the defaults.
  10.  
  11.    OUTPUT: The Event handle if successful, otherwise 0.
  12. */
  13.  
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <malloc.h>
  18. #include <cas.h>
  19. #include <fax.h>
  20.  
  21. extern int pascal FAXSend(char *to,
  22.                    char *PhoneNumber,
  23.                    FILELIST *files,
  24.                    CAS_DATE *date,
  25.                    CAS_TIME *time)
  26. {
  27.   FILELIST *CurrFile = NULL;          /* temp for caller's list of files  */
  28.   FTRLIST *CurrFTRLIST = NULL,
  29.           *NextFTRLIST = NULL,        /* for building the events list of FTR's*/
  30.           *LastFTRLIST;               /* for freeing the FTR list */
  31.   int FileCount;                      /* number of files to send for TCF */
  32.   SFTR *SingleFileSend;               /* In case there is only one file */
  33.   int retval = 0;                     /* default, if anything goes wrong */
  34.   EDB *EDBbuffer;                     /* for CAS call GetExternalData */
  35.   char *TCFfilename = NULL;           /* tmpnam() allocates space for this */
  36.   FILE *fptr;                         /* Stream pointer for TCF */
  37.   WORD CallerTime, CallerDate;        /* Intermediate holders */
  38.   int writ;                           /* To catch errors: # of items written */
  39.   int i;
  40.  
  41.   /* Event attributes saved, just in case they're needed. */
  42.   int CoverLength = DefaultsECS.EventControlFile.FTROffset - 383;
  43.                                     /* to understand '383', see Intel/DCA CAS */
  44.  
  45.   FTRLIST *SaveFileList = DefaultsECS.FirstFTR;
  46.   WORD SaveDate = DefaultsECS.EventControlFile.EventDate;
  47.   WORD SaveTime = DefaultsECS.EventControlFile.EventTime;
  48.   int SaveFileCount = DefaultsECS.EventControlFile.FileCount;
  49.   char SaveDestName[32],
  50.        SaveDestPhone[47];             /* These saved ONLY if necessary */
  51.  
  52.   FAXerrno = CASerrorcode = 0;      /* They keep it if nothing goes wrong */
  53.  
  54.   /* Check the defaults structure, whether we use it or not */
  55.   if (!(ECSOkToSubmit(&DefaultsECS))) {
  56.     FAXerrno = INVALIDDEFAULTS;              /* For now, it's a warning only */
  57.   }
  58.  
  59.   /* convert to DOS file time and date format */
  60.   if (time) {
  61.     if (ValidTime(time)) {
  62.       CallerTime = ((int)time->Second / 2) |
  63.                    ((int)time->Minute << 5) |
  64.                    ((int)time->Hour << 11);
  65.     }
  66.     else {
  67.       FAXerrno = BADEVENTTIME;
  68.       return(retval);
  69.     }
  70.   }
  71.   if (date) {
  72.  
  73.     /* If not all 0's, check for validity before converting */
  74.     if (date->Day || date->Month || date->Year) {
  75.       if (ValidDate(date)) {
  76.         CallerDate = (int)date->Day |
  77.                      ((int)date->Month << 5) |
  78.                      (((int)date->Year - 1980) << 9);
  79.       }
  80.       else {
  81.         FAXerrno = BADEVENTDATE;
  82.         return(retval);
  83.       }
  84.     }
  85.     else {
  86.       CallerDate = 0;
  87.     }
  88.   }
  89.  
  90.   /* If only one file, and none of the fields ignored by the SubmitSingleFile
  91.      function are set, use SubmitSingleFile. */
  92.   if (
  93.       SubmitSingleOk(&DefaultsECS) &&
  94.       (
  95.        (files)? files->next == NULL :
  96.                 DefaultsECS.EventControlFile.FileCount == 1
  97.       )
  98.      ) {
  99.  
  100.     if (!DefaultsECS.FirstFTR) {
  101.       FAXerrno = BADFILECOUNT;
  102.       return(retval);
  103.     }
  104.  
  105.     SingleFileSend = (SFTR *)calloc(sizeof(SFTR) + CoverLength, 1);
  106.     if (!SingleFileSend) {
  107.       FAXerrno = OUTOFMEMORY;
  108.       return(retval);
  109.     }
  110.  
  111.     /* Set SFTR fields from the Defaults and the parameters */
  112.     SingleFileSend->TransferType = DefaultsECS.EventControlFile.TransferType;
  113.     if (SingleFileSend->TransferType != FILE_TRANSFER) { /* it's a fax type */
  114.       SingleFileSend->TextSize =  DefaultsECS.FirstFTR->OneFTR.TextSize;
  115.     }
  116.  
  117.     /* Now do the date and time fields, converting to DOS file format */
  118.     SingleFileSend->EventTime = (time)? CallerTime :
  119.                                         DefaultsECS.EventControlFile.EventTime;
  120.     SingleFileSend->EventDate = (date)? CallerDate :
  121.                                         DefaultsECS.EventControlFile.EventDate;
  122.     strncpy(SingleFileSend->DestinationName,
  123.             (to)? to :
  124.                   DefaultsECS.EventControlFile.DestinationName,
  125.             NAMELENGTH);
  126.     if (SingleFileSend->DestinationName[NAMELENGTH-1]) {
  127.       FAXerrno = STRINGTOOLONG;
  128.       SingleFileSend->DestinationName[NAMELENGTH-1] = '\0';
  129.     }
  130.     strncpy(SingleFileSend->FileName,
  131.             (files)? files->FileName :
  132.                      DefaultsECS.FirstFTR->OneFTR.FileName,
  133.             FULLFNAMELENGTH);
  134.     if (SingleFileSend->FileName[FULLFNAMELENGTH-1]) {
  135.       FAXerrno = STRINGTOOLONG;
  136.       SingleFileSend->FileName[FULLFNAMELENGTH-1];
  137.     }
  138.     if (DefaultsECS.FirstFTR->next) {
  139.       FAXerrno = MOREFTRSTHANFC;
  140.     }
  141.     strncpy(SingleFileSend->Phone,
  142.             (PhoneNumber)? PhoneNumber :
  143.                            DefaultsECS.EventControlFile.Phone,
  144.             PHONENUMLENGTH);
  145.     if (SingleFileSend->Phone[PHONENUMLENGTH-1]) {
  146.       FAXerrno = STRINGTOOLONG;
  147.       SingleFileSend->Phone[PHONENUMLENGTH-1] = '\0';
  148.     }
  149.     strncpy(SingleFileSend->ApplicationTag,
  150.             DefaultsECS.EventControlFile.ApplicationTag,
  151.             APPTAGLENGTH);
  152.     if (SingleFileSend->ApplicationTag[APPTAGLENGTH-1]) {
  153.       FAXerrno = STRINGTOOLONG;
  154.       SingleFileSend->ApplicationTag[APPTAGLENGTH-1] = '\0';
  155.     }
  156.     SingleFileSend->SendCover = DefaultsECS.EventControlFile.SendCover;
  157.     if (CoverLength && DefaultsECS.CoverPageText) {
  158.       strncpy(&SingleFileSend->CoverTextDummy,
  159.               DefaultsECS.CoverPageText,
  160.               CoverLength);
  161.       if ((&SingleFileSend->CoverTextDummy)[CoverLength-1]) {
  162.         FAXerrno = STRINGTOOLONG;
  163.         (&SingleFileSend->CoverTextDummy)[CoverLength-1] = '\0';
  164.       }
  165.     }
  166.  
  167.     /* All done setting up, nothing left but to check and ship it! */
  168.     if (SFTROkToSubmit(SingleFileSend)) {
  169.       retval = CASSubmitSingleFile(SingleFileSend);
  170.       free(SingleFileSend);
  171.       if (retval < 0) {
  172.         FAXerrno = SUBMITSINGLE;
  173.         CASerrorcode = -retval;
  174.         return(0);
  175.       }
  176.     }
  177.     else {
  178.       free(SingleFileSend);
  179.       return(retval);
  180.     }
  181.   }
  182.   /* If NOT doing a SubmitSingle, write Task Control File, submit the event, */
  183.   /*    and restore the original values of the Defaults. */
  184.   else {
  185.  
  186.     /* First, save and assign the destination name and phone, if necessary */
  187.     if (to) {
  188.       strncpy(SaveDestName,
  189.               DefaultsECS.EventControlFile.DestinationName,
  190.               NAMELENGTH);
  191.       if (SaveDestName[NAMELENGTH-1]) {
  192.         FAXerrno = STRINGTOOLONG;
  193.         SaveDestName[NAMELENGTH-1] = '\0';
  194.       }
  195.       strncpy(DefaultsECS.EventControlFile.DestinationName,
  196.               to,
  197.               NAMELENGTH);
  198.       if (DefaultsECS.EventControlFile.DestinationName[NAMELENGTH-1]) {
  199.         FAXerrno = STRINGTOOLONG;
  200.         DefaultsECS.EventControlFile.DestinationName[NAMELENGTH-1] = '\0';
  201.       }
  202.     }
  203.     if (PhoneNumber) {
  204.       strncpy(SaveDestPhone,
  205.               DefaultsECS.EventControlFile.Phone,
  206.               PHONENUMLENGTH);
  207.       if (SaveDestPhone[PHONENUMLENGTH-1]) {
  208.         FAXerrno = STRINGTOOLONG;
  209.         SaveDestPhone[PHONENUMLENGTH-1] = '\0';
  210.       }
  211.       strncpy(DefaultsECS.EventControlFile.Phone,
  212.               PhoneNumber,
  213.               PHONENUMLENGTH);
  214.       if (DefaultsECS.EventControlFile.Phone[PHONENUMLENGTH-1]) {
  215.         FAXerrno = STRINGTOOLONG;
  216.         DefaultsECS.EventControlFile.Phone[PHONENUMLENGTH-1] = '\0';
  217.       }
  218.     }
  219.     if (time)
  220.         DefaultsECS.EventControlFile.EventTime = CallerTime;
  221.     if (date)
  222.         DefaultsECS.EventControlFile.EventDate = CallerDate;
  223.  
  224.     /* Next, if files specified, build new FTRs and attach to DefaultsECS */
  225.     if (files) {
  226.       for (CurrFile = files, FileCount = 0;
  227.            CurrFile;                                /* while more files */
  228.            CurrFile = CurrFile->next, FileCount++) {
  229.  
  230.         NextFTRLIST = (FTRLIST *)calloc(sizeof(FTRLIST), 1); /* make an FTR */
  231.         if (!NextFTRLIST) {
  232.           FAXerrno = OUTOFMEMORY;
  233.           goto restore;
  234.         }
  235.         if (!CurrFTRLIST) {           /* If the first, make it so */
  236.           CurrFTRLIST = DefaultsECS.FirstFTR = NextFTRLIST;
  237.         }
  238.         else {          /* otherwise connect it up to the last one allocated */
  239.           CurrFTRLIST = CurrFTRLIST->next = NextFTRLIST;
  240.         }
  241.  
  242.         strncpy(CurrFTRLIST->OneFTR.FileName,
  243.                 CurrFile->FileName,
  244.                 FULLFNAMELENGTH);
  245.         if (CurrFTRLIST->OneFTR.FileName[FULLFNAMELENGTH-1]) {
  246.           FAXerrno = STRINGTOOLONG;
  247.           CurrFTRLIST->OneFTR.FileName[FULLFNAMELENGTH-1] = '\0';
  248.         }
  249.         CurrFTRLIST->next = NULL;
  250.  
  251.         /* IF FAXing, set fax-specific fields from CurrFile AND Defaults FTR */
  252.         if (DefaultsECS.EventControlFile.TransferType != FILE_TRANSFER) {
  253.           CurrFTRLIST->OneFTR.FileType = CurrFile->FileType;
  254.           CurrFTRLIST->OneFTR.TextSize = DefaultsFTRL.OneFTR.TextSize;
  255.           CurrFTRLIST->OneFTR.AddPageIncrements = DefaultsFTRL.OneFTR.AddPageIncrements;
  256.           CurrFTRLIST->OneFTR.PageLength = DefaultsFTRL.OneFTR.PageLength;
  257.         }  /* no else: just leaving them all 0's is correct for file transfers */
  258.  
  259.       }                                /* end of while (more files) */
  260.       DefaultsECS.EventControlFile.FileCount = FileCount;
  261.     }   /* end of if (files); no else:  FirstFTR already points to an FTRLIST */
  262.  
  263.     /* If any of the fields common with the External Data Block are not       */
  264.     /*    initialized, get the EDB. */
  265.     if ((DefaultsECS.EventControlFile.SenderName[0] == '\0') ||
  266.         (DefaultsECS.EventControlFile.LogoFilePath[0] == '\0')) {
  267.       EDBbuffer = (EDB *)malloc(sizeof(EDB));
  268.       if (!EDBbuffer) {
  269.         FAXerrno = OUTOFMEMORY;
  270.         goto restore;
  271.       }
  272.       if (CASGetExternalData(EDBbuffer)) {
  273.         FAXerrno = GETEDB;  /* Warning only: too bad, but we can still go on. */
  274.         free(EDBbuffer);
  275.       }
  276.       else {
  277.         if (DefaultsECS.EventControlFile.SenderName[0] == '\0') {
  278.           strncpy(DefaultsECS.EventControlFile.SenderName,
  279.                   EDBbuffer->DefaultSender,
  280.                   NAMELENGTH);
  281.           if (DefaultsECS.EventControlFile.SenderName[NAMELENGTH-1]) {
  282.             FAXerrno = STRINGTOOLONG;
  283.             DefaultsECS.EventControlFile.SenderName[NAMELENGTH-1] = '\0';
  284.           }
  285.         }
  286.         if (DefaultsECS.EventControlFile.LogoFilePath[0] == '\0') {
  287.           strncpy(DefaultsECS.EventControlFile.LogoFilePath,
  288.                   EDBbuffer->DefaultDir,
  289.                   FULLFNAMELENGTH);    /* This pads out full field to '\0's */
  290.           if (DefaultsECS.EventControlFile.LogoFilePath[DIRPATHLENGTH-1]) {
  291.             FAXerrno = STRINGTOOLONG;
  292.             DefaultsECS.EventControlFile.LogoFilePath[DIRPATHLENGTH-1] = '\0';
  293.           }
  294.           strncat(DefaultsECS.EventControlFile.LogoFilePath,
  295.                   EDBbuffer->DefaultLogo,
  296.                   FNAMELENGTH);
  297.           if (DefaultsECS.EventControlFile.LogoFilePath[FULLFNAMELENGTH-1]) {
  298.             FAXerrno = STRINGTOOLONG;
  299.             DefaultsECS.EventControlFile.LogoFilePath[FULLFNAMELENGTH-1] = '\0';
  300.           }
  301.         }
  302.         free(EDBbuffer);
  303.       }
  304.     }
  305.  
  306.     /* The Event Control Structure is complete:  Check it and BUILD the TCF! */
  307.  
  308.     if (ECSOkToSubmit(&DefaultsECS)) {
  309.  
  310.       /* Get the file ready:  name, open and write to it. */
  311.       TCFfilename = tmpnam(NULL);
  312.  
  313.       if((fptr = fopen(TCFfilename, "wb")) == NULL) {
  314.          FAXerrno = CANTOPENFILE;
  315.          goto restore;
  316.       }
  317.       /* Write the TCF data to disk, starting with the Event control structure. */
  318.       writ = fwrite(&DefaultsECS.EventControlFile, sizeof(ECF), 1, fptr);
  319.       if (writ < 1) {
  320.         FAXerrno = CANTWRITEFILE;
  321.         fclose(fptr);
  322.         goto restore;
  323.       }
  324.  
  325.       /* Whether to write cover text depends only on CoverLength, not SendCover*/
  326.       if (CoverLength) {
  327.         writ = fwrite(DefaultsECS.CoverPageText, CoverLength, 1, fptr);
  328.         if (writ < 1) {
  329.           FAXerrno = CANTWRITEFILE;
  330.           fclose(fptr);
  331.           goto restore;
  332.         }
  333.       }
  334.  
  335.       /* Finally, write the File Transfer Records */
  336.       for (FileCount = 0, CurrFTRLIST = DefaultsECS.FirstFTR;
  337.            FileCount < DefaultsECS.EventControlFile.FileCount;
  338.            FileCount++, CurrFTRLIST = CurrFTRLIST->next) {
  339.  
  340.         if (!CurrFTRLIST) {
  341.           FAXerrno = BADFILECOUNT;
  342.           fclose(fptr);
  343.           goto restore;
  344.         }
  345.         writ = fwrite(&CurrFTRLIST->OneFTR, sizeof(FTR), 1, fptr);
  346.         if (writ < 1) {
  347.           FAXerrno = CANTWRITEFILE;
  348.           fclose(fptr);
  349.           goto restore;
  350.         }
  351.       }
  352.       if (CurrFTRLIST) {
  353.         FAXerrno = MOREFTRSTHANFC;            /* warning only */
  354.       }
  355.  
  356.       if (fclose(fptr)) {
  357.         FAXerrno = CANTCLOSEFILE;
  358.         goto restore;
  359.       }
  360.  
  361.       /* Finally, submit! */
  362.       retval = CASSubmitTask(TCFfilename);
  363.       if (retval < 0) {
  364.         FAXerrno = SUBMITTASK;
  365.         CASerrorcode = -retval;
  366.         retval = 0;
  367.         goto restore;
  368.       }
  369.     }
  370. restore:
  371.     /* Restore defaults to Defaults structure */
  372.     if (TCFfilename) {
  373.         remove(TCFfilename);
  374.     }
  375.     if (to) {
  376.       strncpy(DefaultsECS.EventControlFile.DestinationName,
  377.               SaveDestName,
  378.               NAMELENGTH);
  379.       if (DefaultsECS.EventControlFile.DestinationName[NAMELENGTH-1]) {
  380.         FAXerrno = STRINGTOOLONG;
  381.         DefaultsECS.EventControlFile.DestinationName[NAMELENGTH-1] = '\0';
  382.       }
  383.     }
  384.     if (PhoneNumber) {
  385.       strncpy(DefaultsECS.EventControlFile.Phone,
  386.               SaveDestPhone,
  387.               PHONENUMLENGTH);
  388.       if (DefaultsECS.EventControlFile.Phone[PHONENUMLENGTH-1]) {
  389.         FAXerrno = STRINGTOOLONG;
  390.         DefaultsECS.EventControlFile.Phone[PHONENUMLENGTH-1] = '\0';
  391.       }
  392.     }
  393.     if (time) {
  394.       DefaultsECS.EventControlFile.EventTime = SaveTime;
  395.     }
  396.     if (date) {
  397.       DefaultsECS.EventControlFile.EventDate = SaveDate;
  398.     }
  399.     if (DefaultsECS.FirstFTR != SaveFileList) {
  400.  
  401.       /* First, free up the FTR list we created, from the end backwards */
  402.       free_FTRLIST(DefaultsECS.FirstFTR);
  403.  
  404.       /* Finally, restore originals */
  405.       DefaultsECS.FirstFTR = SaveFileList;
  406.       DefaultsECS.EventControlFile.FileCount = SaveFileCount;
  407.     }
  408.  
  409.   }  /* end of else: there were multiple files, so use a TCF */
  410.   return(retval);   /* And we're ATTA-HERE!! */
  411. }
  412.